#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>

void interpret(char *in, char **out);
void execute(char **tar);
void run(char *tar);
void fullexecute(char **tar);

void runCommand(char *tar){
    char *argumentArray[255];
    interpret(tar, argumentArray);
    execute(argumentArray);
}

void interpret(char *in, char **out){
    char *x, *y;
    x = in;
    strcpy(x,strsep(&x, "\n"));
    int i = 0;
    while(y = strsep(&x," ")){
        if(*y){
            out[i] = y;
            printf("Out[%d] is: %s.\n",i,out[i]);
            i++;
        }
    }
    out[i++] = NULL;
/*
    for(i = 0; y = strsep(&x," "); i++){
        out[i] = y;
        printf("Out[%d] is: %s.\n",i,out[i]);
    }
    out[i++] = NULL;
*/
/*
    int i = 0;
    while(y = strsep(x," ")){
        out[i] = y;
        i++
    }
*/
}

//add semicolon
//add redirection: done through messing around with stdin/stdout and tail recursion

void execute(char **tar){//basic execute command
    char temp[255] = "/bin/";
    strncat(temp, tar[0], 250);
    int childPiD, waitStatus;
    childPiD = fork();
    if (childPiD == 0){
        execv(temp, tar);
    }
    else{
        wait(&waitStatus);
    }
}

void fullexecute(char **tar) {//full recursive execute command
}
